package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author wzh
 * @since 2022-09-23
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;
    @Override
    public void removeByCourseId(String courseId) {
        //
        QueryWrapper<EduVideo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.select("video_source_id");
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        List<EduVideo> eduVideos = baseMapper.selectList(queryWrapper);
//        List<String> VideoIds=eduVideos.stream().map(EduVideo::getVideoSourceId).collect(Collectors.toList());
        List<String> VideoIds=new ArrayList<>();
        for (EduVideo eduVideo : eduVideos) {
            String videoSourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId))
                VideoIds.add(videoSourceId);
        }
        vodClient.deleteBatch(VideoIds);
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
